var express = require('express');
var router = express.Router();
var fs = require('fs');
var multipart = require('connect-multiparty');
var multiparty = require('multiparty'); 
  
var multipartMiddleware = multipart();

var add = require('../services/index/test');  

/* GET home page. */
router.get('/', function(req, res, next) {
  res.render('index', { title: 'mobile web' + add (1,2) });
});

router.get('/get_test_data', function(req, res) {
  res.json({data: 'get test data success !'});
});

router.get('/camera', function(req, res) {
  res.render('camera', {});
});

router.get('/test', function (req, res) {
  res.render('test', {});
});


router.get('/create_img', function(req, res) {
});

router.post('/upload_file', multipartMiddleware, function(req, res) {
  var a = req.body.a;
  var b = req.body.b;

  var filename = req.body.filename;

  var file = req.files;

  console.log(file);
  
  console.log(filename,a,b);
  res.send('' + a + ',' + b);
});

router.post('/form_upload_file', multipartMiddleware, function (req, res) {
  var num1 = req.body.num1;
  var num2 = req.body.num2;
  var filename = req.body.filename;
  var file = req.files;
  console.log(file);
  console.log(num1, num2, filename);

  //生成multiparty对象，并配置上传目标路径
  var form = new multiparty.Form({ uploadDir: './public/files/' });
  //上传完成后处理
  form.parse(req, function (err, fields, files) {
    var filesTmp = JSON.stringify(files, null, 2);

    if (err) {
      console.log('parse error: ' + err);
    } else {
      console.log('parse files: ' + filesTmp);
      var inputFile = files.inputFile[0];
      var uploadedPath = inputFile.path;
      var dstPath = './public/files/' + inputFile.originalFilename;
      //重命名为真实文件名
      fs.rename(uploadedPath, dstPath, function (err) {
        if (err) {
          console.log('rename error: ' + err);
        } else {
          console.log('rename ok');
        }
      });
    }

    // res.writeHead(200, { 'content-type': 'text/plain;charset=utf-8' });
    // res.write('received upload:\n\n');
    // res.end(util.inspect({ fields: fields, files: filesTmp }));
  });

  res.send("form_upload_file success !");
});

router.post('/uploading', function (req, res, next) {
  //生成multiparty对象，并配置上传目标路径
  var form = new multiparty.Form({ uploadDir: './public/files/' });
  //上传完成后处理
  form.parse(req, function (err, fields, files) {
    var filesTmp = JSON.stringify(files, null, 2);

    if (err) {
      console.log('parse error: ' + err);
    } else {
      console.log('parse files: ' + filesTmp);
      var inputFile = files.inputFile[0];
      var uploadedPath = inputFile.path;
      var dstPath = './public/files/' + inputFile.originalFilename;
      //重命名为真实文件名
      fs.rename(uploadedPath, dstPath, function (err) {
        if (err) {
          console.log('rename error: ' + err);
        } else {
          console.log('rename ok');
        }
      });
    }

    res.writeHead(200, { 'content-type': 'text/plain;charset=utf-8' });
    res.write('received upload:\n\n');
    res.end();
  });
});


router.get('/login', function(req, res, next) {
  res.render('login', {});
});

module.exports = router;
